package com.liangzhong.view
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.greensock.TweenLite;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.manager.ResourceManager;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;

	/**
	 *比赛模块加载条 
	 * @author lake
	 * 
	 */	
	public class MatchLoadinglProgressBar extends Sprite
	{
		private static var _instance :MatchLoadinglProgressBar;
		/**
		 *皮肤 
		 */		
		public var symbolSkin		:MovieClip;
		/**
		 *背景动画 
		 */		
		protected var bgActionMc :MovieClip;
		/**
		 *加载资源名 
		 */		
		protected var nameTxt 	:TextField;
		/**
		 *加载百分比 
		 */		
		protected var percentTxt :TextField;
		/**
		 *提示mc 
		 */		
		protected var tipsMc		:MovieClip;
		/**
		 *加载提示 
		 */		
		protected var textTipInfos :XMLList;
		/**
		 *tip显示顺序索引 
		 */		
		protected var tipShowIndes :Array;
		/**
		 *正在显示的index 
		 */		
		protected var curShowIndex :int;
		
		public function MatchLoadinglProgressBar()
		{
			initUI();
		}
		/**
		 *获取单例 
		 * @return 
		 * 
		 */		
		public static function get instance():MatchLoadinglProgressBar
		{
			if(_instance == null)
			{
				_instance = new MatchLoadinglProgressBar();
			}
			return _instance;
		}
		/**
		 * 初始化
		 * @param view
		 * 
		 */		
		public function initUI(view:Object=null):void
		{
			symbolSkin = Reflection.createInstance("com.lz.match.LoadingMC");
			if(symbolSkin == null)
			{
				return;
			}
			//nameTxt = symbolSkin.nameTxt;
			percentTxt = symbolSkin.percentTxt;
			bgActionMc = symbolSkin.bgActionMc;
			bgActionMc.gotoAndStop(0);
			tipsMc = Reflection.createInstance("com.lz.loading.TipsMc");
			this.addChild(symbolSkin);
			var tipConfig :XML = ResourceManager.instance.getResource(ResourceConst.TEXT_TIPS_CONFIG);
			if(tipConfig)
			{
				textTipInfos = tipConfig.loadText[0].item;
			}
			addEvtListener();
		}
		/**
		 * 添加事件
		 * 
		 */		
		public function addEvtListener():void
		{
			this.addEventListener(Event.ADDED_TO_STAGE,onAddHandler);
			this.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveHandler);
		}
		/**
		 * 删除事件
		 * 
		 */		
		public function removeEvtListener():void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE,onAddHandler);
			this.removeEventListener(Event.REMOVED_FROM_STAGE,onRemoveHandler);
		}
		/**
		 * onAddHandler
		 * @param e
		 * 
		 */		
		private function onAddHandler(e :Event):void
		{
			if(bgActionMc)bgActionMc.gotoAndPlay(0);
		}
		/**
		 * onRemoveHandler
		 * @param e
		 * 
		 */		
		private function onRemoveHandler(e :Event):void
		{
			if(bgActionMc)bgActionMc.gotoAndStop(0);
		}
		/**
		 *设置名字 
		 * @param value
		 * 
		 */		
		public function setResName(value :String = ""):void
		{
			//nameTxt.htmlText = value;
		}
		/**
		 *设置百分比 
		 * @param value
		 * 
		 */		
		public function setPercent(value :String = "1%"):void
		{
			if(percentTxt)
			{
				percentTxt.htmlText = value;
			}
		}
		/**
		 *初始化tips 
		 * 
		 */		
		private function initShowTips():void
		{
			var randomCounts :Array = [];
			tipShowIndes = [];
			curShowIndex = 0;
			var i :int = 0;
			var len :int = textTipInfos.length();
			for(i = 0;i < len;i++)
			{
				randomCounts.push(i);
			}
			var lenS :int;
			var index :int;
			for(i = 0;i < len;i++)
			{
				lenS = randomCounts.length;
				index = Math.random() * lenS;
				tipShowIndes.push(randomCounts[index]);
				randomCounts.splice(index,1);
			}
		}
		/**
		 * 
		 * 开始加载
		 */		
		public function startLoading():void
		{
			if(textTipInfos == null)
			{
				return;
			}
			initShowTips();
			if(tipsMc)
			{
				this.addChild(tipsMc);
				startTips();
			}
		}
		/**
		 *开始tips动作
		 * 
		 */		
		private function startTips():void
		{
			tipsMc.txtText.htmlText = "小提示：" + String(textTipInfos[tipShowIndes[curShowIndex]]);
			tipsMc.txtText.width = tipsMc.txtText.textWidth + 10;
			curShowIndex ++ ;
			if(curShowIndex >= tipShowIndes.length)
			{
				curShowIndex = 0;
			}
			tipsMc.ballMc.x = tipsMc.txtText.width - 10;
			//////////////  分割线 //////////////////////////////
			tipsMc.x = -tipsMc.width;
			tipsMc.y = 544;
			tipsMc.alpha = 0;
			var showX :Number = (GlobalDef.GameWidth - tipsMc.width)/2;
			TweenLite.to(tipsMc,1,{x:showX,alpha:1});
			TweenLite.to(tipsMc,1,{x:GlobalDef.GameWidth,alpha:0,delay:9,onComplete:onOneTipsComplete,overwrite:0});
		}
		/**
		 *显示完一条tips 
		 * 
		 */		
		private function onOneTipsComplete():void
		{
			TweenLite.killTweensOf(tipsMc);
			startTips();
		}
		/**
		 * 停止加载
		 * 
		 */		
		public function stopLoading():void
		{
			if(tipsMc)
			{
				if(tipsMc.parent)
				{
					tipsMc.parent.removeChild(tipsMc);
				}
				TweenLite.killTweensOf(tipsMc);
			}
		}
		/**
		 * 销毁
		 * 
		 */		
		public function destroyView():void
		{
			removeEvtListener();
			_instance = null;
			DisplayObjectUtil.removeAllChildren(this);
		}
	}
}